package org.academiadecodigo.carcrash.cars;

import org.academiadecodigo.carcrash.Utils;
import org.academiadecodigo.carcrash.field.Direction;
import org.academiadecodigo.carcrash.field.Position;

abstract public class Car {

    /** The position of the car on the grid */
    private Position pos;
    private boolean crashed;
    private CarType brand;
    private int maxSpeed;
    private Position destination;
    private Direction direction;

    public Car(CarType brand, int maxSpeed) {
        this.pos = new Position();
        this.crashed = false;
        this.brand = brand;
        this.maxSpeed = maxSpeed;
        setNewDestination();
        this.direction = Direction.UP;
    }

    public Position getPos() {
        return pos;
    }

    public Position getDest() {
        return this.destination;
    }

    public boolean isCrashed() {
        return this.crashed;
    }

    public void hasCrashed() {
        this.crashed = true;
    }

    public int getMaxSpeed() {
        return this.maxSpeed;
    }

    public void move() {
        if (isCrashed())
            return;
        this.direction = getPos().getClosestToDirection(getDest(), this.direction);
        if (getDest().equals(getPos())) {
            setNewDestination();
        } else {
            getPos().move(this.direction, 1);
        }
    }

    public void setNewDestination() {
        this.destination = new Position();
    }

    @Override
    public String toString() {
        return isCrashed() ? "C" : this.brand.toLetter();
    }
}
